package com.example.job.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.db.model.IPage;
import com.example.job.JobUtils;
import com.example.job.model.JobModel;
import com.example.job.service.JobModelService;
import com.example.utils.Results;

/**
 * @Classname JobModelController
 * @Description TODO
 * @Date 2022/8/2 21:38
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/jobmodel")
public class JobModelController {

    @Autowired
    JobModelService jobModelService;

    @PostMapping("/page")
    public JSONObject pageController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                     @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                     @Param(value = "searchParams",required = false)JSONObject object){
        IPage<JobModel> jobModel=jobModelService.page(pageNum,pageSize,object);
        return Results.successLayuiTable(jobModel);
    }

    @GetMapping(value = "/stop",desc = "停止定时任务")
    public JSONObject stopController(@Param(value = "id")String id){
        JobModel jobModel=jobModelService.getById(id);
        if(jobModel==null){
            return Results.error("调度任务不存在");
        }
        try {
            JobUtils.stopJob(jobModel);
            return Results.success("任务定制成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Results.error("任务停止失败");
    }

    @GetMapping(value = "/start",desc = "启动定时任务")
    public JSONObject startController(@Param(value = "id")String id){
        JobModel jobModel = jobModelService.getById(id);
        if (jobModel == null) {
            return Results.error("调度任务不存在");
        }
        try {
            JobUtils.startJob(jobModel);
            return Results.success("任务启动成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Results.error("任务停止失败");
    }

    @PostMapping("/save")
    public JSONObject saveController(@RequestBody JobModel jobModel){
        String jobName = jobModel.getJobName();
        String jobGroup=jobModel.getJobGroup();
        String tiggerName=jobModel.getJobTigger();
        String tiggerGroup=jobModel.getJobGroup();
        boolean flag=JobUtils.classIsExist(jobModel.getClazz());
        if(!flag){
            return Results.error(jobModel.getClazz()+"类未发现");
        }
        if(jobName==null){
            return Results.error("jobName不得为空");
        }
        if(jobGroup==null){
            return Results.error("jobGroup不得为空");
        }
        if(tiggerName==null){
            return Results.error("tiggerName不得为空");
        }
        if (tiggerGroup == null) {
            return Results.error("tiggerGroup不得为空");
        }
        JSONObject obj=new JSONObject();
        obj.put("jobName",jobName);
        obj.put("jobGroup",jobGroup);
        obj.put("jobTigger",tiggerName);
        obj.put("jobTiggerGroup",tiggerGroup);
        JobModel model=jobModelService.getJobModelorJSONObject(obj);
        if(model!=null){
            if(model.getJobGroup().equals(jobGroup)){
                return Results.error("存在同名的jobGroup");
            }else if(model.getJobName().equals(jobName)){
                return Results.error("存在同名的jobName");
            }else if (model.getJobTigger().equals(tiggerName)) {
                return Results.error("存在同名的tiggerName");
            }else if(model.getJobTiggerGroup().equals(tiggerGroup)){
                return Results.error("存在同名的tiggerGroup");
            }else {
                return Results.error("系统错误,请联系管理员");
            }
        }
        jobModelService.save(jobModel);
        try {
            JobUtils.addJob(jobModel);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Results.success("保存成功");
    }

    @PostMapping("/update")
    public JSONObject updateController(@RequestBody JobModel jobModel) throws ClassNotFoundException {
        String jobName = jobModel.getJobName();
        String jobGroup=jobModel.getJobGroup();
        String tiggerName=jobModel.getJobTigger();
        String tiggerGroup=jobModel.getJobGroup();

        boolean flag=JobUtils.classIsExist(jobModel.getClazz());
        if(!flag){
            return Results.error(jobModel.getClazz()+"类未发现");
        }
        if(jobName==null){
            return Results.error("jobName不得为空");
        }
        if(jobGroup==null){
            return Results.error("jobGroup不得为空");
        }
        if(tiggerName==null){
            return Results.error("tiggerName不得为空");
        }
        if (tiggerGroup == null) {
            return Results.error("tiggerGroup不得为空");
        }
        JSONObject obj=new JSONObject();
        obj.put("jobName",jobName);
        obj.put("jobGroup",jobGroup);
        obj.put("jobTigger",tiggerName);
        obj.put("jobTiggerGroup",tiggerGroup);
        JobModel model=jobModelService.getJobModelorJSONObject(obj);
        if(model!=null){
            if(jobModel.getId()==null){
                if(model.getJobGroup().equals(jobGroup)){
                    return Results.error("存在同名的jobGroup");
                }else if(model.getJobName().equals(jobName)){
                    return Results.error("存在同名的jobName");
                }else if (model.getJobTigger().equals(tiggerName)) {
                    return Results.error("存在同名的tiggerName");
                }else if(model.getJobTiggerGroup().equals(tiggerGroup)){
                    return Results.error("存在同名的tiggerGroup");
                }else {
                    return Results.error("系统错误,请联系管理员");
                }
            }
        }
        try {
            JobUtils.stopJob(jobModel);
            JobUtils.deleteJob(jobModel);
            JobUtils.addJob(jobModel);
        }catch (Exception e) {
            e.printStackTrace();
        }
        jobModelService.updateById(jobModel);
        return Results.success("更新成功");
    }

    @GetMapping("/getone")
    public JSONObject getController(@Param("id")String id){
        JobModel jobModel=jobModelService.getById(id);
        return Results.success(jobModel);
    }
}
